<?php
/**
 * 自定义上传通用类
 */
class MyUploadFile{

/**
 * 上传功能对象
 * @var obj
 */
    private $upload=null;

/**
 * 上传结束返回内容
 * @var array
 */
    private $uploadFileInfo=array();

/**
 * 上传类型
 * @var string 默认upload
 * upload 基于内容图片  thumb 缩放图  gallery 产品图
 */
    private $uploadType='upload';

/**
 * 图片上传根目录
 * @var string
 */
    private $savePath='';

    private $original='';

    private $allowExts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型

    private $maxSize=3145728;// 设置附件上传大小

    private $uploadReplace=true;//存在同名文件是否是覆盖

/**
 * 各种set函数
 */
    public function setAllowExts($arr)
    {
        $this->allowExts=$arr;
    }

    public function setMaxSize($size)
    {
        $this->maxSize=$size;
    }      

/**
 * 构造函数
 * @access public
 * @param string $type 上传图片类型,默认upload
 * upload-内容中上传图 thumb-缩放图 gallery-产品图
 * @param array $thumbConfig 缩略图参数  
 */
    public function __construct($type='upload',$thumbConfig=array(),$isThumb=true)
    {
        import("@.ORG.UploadFile");
        $this->savePath=C('MY_UPLOAD_PATH');
        $this->original=C('MY_UPLOAD_ORIGINAL_PATH');
        $this->uploadType=$type;
        $this->savePath=$this->savePath.$this->uploadType.'/';
        $this->upload = new UploadFile();
        $this->upload->maxSize  =$this->maxSize ;
        $this->upload->allowExts  = $this->allowExts;
        $this->upload->uploadReplace=$this->uploadReplace;
        if((($this->uploadType!='upload' && !empty($thumbConfig))|| $this->uploadType=='thumb')&&$isThumb)
        {
            $this->setThumb($thumbConfig);
        }else
            {
                $this->upload->autoSub=true;//是否使用子目录保存上传文件
                $this->upload->subType='date';//子目录创建方式，默认为hash，可以设置为hash或者date
                $this->upload->dateFormat='Ymd';//子目录方式为date的时候指定日期格式
                $this->upload->savePath =$this->savePath;
            }
    }

/**
 * 设置生成缩放图参数
 * @param array $config 缩略图参数 例子：$config['width']=10 $config['height']=10 $config['prefix']='t_'
 * 和TP手册设置方法一直
 * @access private
 */
    private function setThumb($config=array())
    {
        if($this->uploadType!='thumb')
        {
            $this->original=$this->savePath;
            $this->savePath=C('MY_UPLOAD_PATH').'thumb/';

        }
        $this->upload->imageClassPath='@.ORG.Image';
        $this->upload->thumbPath=$this->savePath;
        $this->upload->savePath =$this->original;
        $this->upload->thumb=true;
        if(!empty($config))
        {
            $this->upload->thumbMaxWidth=$config['width'];
            $this->upload->thumbMaxHeight=$config['height'];
            $this->upload->thumbPrefix=$config['prefix'];
        }else
            {
                $config=C('THUMB_SIZE');
                $this->upload->thumbMaxWidth=$config['width'];
                $this->upload->thumbMaxHeight=$config['height'];
                $this->upload->thumbPrefix=$config['prefix'];
            }
    }

/**
 * 执行上传
 * @access public
 */
    public function run_upload()
    {
        if(!$this->upload->upload())
        {
            $this->uploadFileInfo=$this->upload->getErrorMsg();
        }else
            {
                $this->uploadFileInfo=$this->upload->getUploadFileInfo();
            }
    }

    public function getUploadFileInfo()
    {
        return $this->uploadFileInfo;
    }

}
?>